package com.mtt.erp.goods.controller;


import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.google.common.collect.Maps;
import com.mtt.erp.common.annotation.Log;
import com.mtt.erp.common.api.R;
import com.mtt.erp.common.api.ResultCode;
import com.mtt.erp.goods.dto.MabFileCreateDto;
import com.mtt.erp.goods.service.IMabFileService;
import com.mtt.erp.goods.vo.MabFileVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.List;
import java.util.Map;

/**
 * @Description: 商品子表Controller
 * @Author: guomengzhou
 * @CreateDate: 2019/6/3$ 12:50$
 * @Version: 1.0
 */
@Slf4j
@RestController
@RequestMapping("/mabFile")
@Api(value = "子商品接口",tags = "M002-商品库-子商品信息相关接口")
public class MabFileController {
    @Autowired
    private IMabFileService iMabFileService;


    /**
     * 添加子商品
     * @author guomengzhou
     */
    //@Log("添加子商品")
    @PostMapping
    @RequiresPermissions("mabFile:add")
    @ApiOperation(value = "新增子商品", notes = "将子商品类型保存到数据库,mabCreateDto", position = 1)
    public R addMaFile(@Valid MabFileCreateDto mabCreateDto)  {
        try {
            this.iMabFileService.createMabFile(mabCreateDto);
            return R.success(ResultCode.SUCCESS);
        } catch (Exception e) {
            return R.fail(ResultCode.FAILURE);
        }

    }

    @Log("删除子商品")
    @DeleteMapping("/{mab00s}")
    @RequiresPermissions("mabFile:delete")
    @ApiOperation(value = "删除子商品信息", notes = "批量删除子商品信息,传入要删除的子商品id串,以逗号分隔", position = 3)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mab00s", value = "id串,以逗号分割", required=true,paramType = "path")
    })
    public R deleteDepts(@NotBlank(message = "{required}") @PathVariable String mab00s) {
        try {
            String[] ids = mab00s.split(StringPool.COMMA);
            this.iMabFileService.deleteMabFile(ids);
            return R.success(ResultCode.SUCCESS);
        } catch (Exception e) {
            return R.fail(ResultCode.FAILURE);
        }
    }

    /**
     * 查询商品下的子商品
     * @author guomengzhou
     */
    //@Log("查询商品下的子商品")
    @GetMapping("/{mab01}")
    @ApiOperation(value = "获取子商品信息", notes = "通过商品SKU获取子商品信息", position = 2)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mab01", value = "商品SKU", required=true,paramType = "path")
    })
    public R<List<MabFileVo>> mabFileListByMab01(@NotBlank(message = "{required}") @PathVariable String mab01) {
        Map<String,Object> result = Maps.newHashMap();
        List<MabFileVo> list = this.iMabFileService.findMabFileListByMab01(mab01);
        if(!list.isEmpty()){
            return R.data(list);
        }else{
            return R.fail(ResultCode.NO_CONTENT);
        }
    }

}
